package com.hcl.orm.core.dialect.handler;

import com.hcl.orm.core.annotation.DBType;
import com.hcl.orm.core.dialect.SqlDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author hucl
 * @projectName hcl-orm
 * @description: 抽象方言处理类
 * @date 2021/8/1113:21
 */
public abstract class AbstractDialectHandler {
    private static final Logger logger = LoggerFactory.getLogger(AbstractDialectHandler.class);
    /**
     * 下一处理器实例
     */
    private AbstractDialectHandler nextHandler = null;
    /**
     * 当前处理器名称
     */
    private String name;

    public AbstractDialectHandler(String name) {
        this.name = name;
    }

    public final void handle(SqlDefinition sqlDefinition) {
        if (this.needHandled(sqlDefinition)) {
            this.doHandle(sqlDefinition);
        }
        if (this.nextHandler != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("当前处理器:" + this.name);
                logger.debug("处理结果:" + sqlDefinition.toString());
            }
            this.nextHandler.handle(sqlDefinition);
        }
    }

    /**
     * 设置下一个处理者
     */
    public void setNextHandler(AbstractDialectHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    /**
     * 是否需要处理
     *
     * @param sqlDefinition
     * @return
     */
    protected abstract boolean needHandled(SqlDefinition sqlDefinition);

    /**
     * 定义链中每个处理者具体的处理方式
     *
     * @param sqlDefinition
     */
    protected abstract void doHandle(SqlDefinition sqlDefinition);

    /**
     * 匹配的数据库类型
     * @return
     */
    protected abstract List<DBType> getTargetDB();
}
